	var n=1;
	var boxID=0;
	var url;
	var maxN=0;
	var UserId;
	var wordsCount=0;
	var maxWords = 5;
	
	//Get picture where id=n
	function getPicture(){	
		
		$.ajax({
			url: "functions.php",
			type: "POST",
			dataType: "json",
			data: {
				getPicture: n
			},
			success: function(data){
				url = data;
					$("#Picture").attr("src", data);
					
				
			},
			error: function(err1, err2, err3){
				console.warn(err1);
				console.warn(err2);
				console.error(err3);
			}
		});
		n++;
	}
	
	var taboWords = (function(){
		var array;
		function load(){
			$.ajax({
				async: false,
				url: "functions.php",
				type: "POST",
				dataType: "json",
				data: {
					getTaboo: n-1
				},
				success: function(data){
					array=data;
				}
			});
		}
		return {
			load : function(){
				if(array) return;
				load();
			},
			getTaboWords: function(){
				if(!array) load();
				return array;
			},
			refresh: function(){
				load();
			},
			exists: function(word){
				load();
				for(i in array){
					if(array[i]==word){
						return true;
					}
				}
				return false;
			}
		}
	})();
	
	//Function for getting tottal count of pictures
	var maxNdata = (function(){
		var max;
		function load(){
			$.ajax({
				async: false,
				url: "functions.php",
				type: "POST",
				dataType: "json",
				data: {
					getPicturesLenght: "annapikkus"
				},
				success: function(data){
					max=parseInt(data);					
				}
			});
		}
		return {
			load :function(){
				if(max) return;
				load();
			},
			getMaxN: function(){
				if(!max) load();
				return max;
			}
		}
	})();
	
	var answerArray =(function(){
			var array = [];
			function load(){
				array = [];
				$.each($("#wordGroup").find('.single-answer'), function(){
					array.push($( this ).text());
				});
			}
			return{
				load : function(){
					if(array) load();
					return array;
				},
				getAnswerArray : function(){
					load();
					return array;
				},
				refresh: function(){
					load();
				},
				exists: function(word){
					load();
					for(var i=0; i<array.length; i++){
						if(array[i]==word){
							return true;
						}
					}
					return false;
				},
				getMatchingWord: function(){
					load();
					for(var i=0; i<array.length; i++){
						if(array[i]==word){
							return array[i];
						}
					}
				}
				
			}
		})();
	
	function addAnswer(){
		//create New div with answer
		var value = document.getElementById("answer_textbox").value;
		
		if(value.length>0){
		
			var cssstyle = {		background: "#f27b7b",
									transition: "all 2s",
									"-moz-transition": "all 2s",
									"-webkit-transition": "all 2s",
									"-o-transition": "all 2s",
									"ms-transition": "all 2s"
							};
							
			var cssstyle2 = {		background: "white",
									transition: "all 2s",
									"-moz-transition": "all 2s",
									"-webkit-transition": "all 2s",
									"-o-transition": "all 2s",
									"ms-transition": "all 2s"
							};
			var cssstyleword = {	background: "#8ac960",
									transition: "all 2s",
									"-moz-transition": "all 2s",
									"-webkit-transition": "all 2s",
									"-o-transition": "all 2s",
									"ms-transition": "all 2s"
							};
			
			answerArray.refresh();
			
			//check if answer exists in previous answers or tabowords
			if(answerArray.exists(value)||taboWords.exists(value)){
				console.log("exists");
				if(answerArray.exists(value)){
					$.each($("#wordGroup").find('.single-answer'), function(){
							if($( this ).text()==value){
								$( this ).parent().css(cssstyleword);
								//$( this ).parent().css(cssstyle2);
							}
					});
				}
				if(taboWords.exists(value)){
					$.each($("#tabooWords").find('.single-taboword'), function(){
						if($( this ).text()==value){
							$( this ).parent().css(cssstyle);
							//$( this ).css(cssstyle2);
						}
					});
				}
			}else{
			
				if(wordsCount < maxWords){
					
					var div1=document.getElementById("wordGroup");
					var div2=document.createElement("div");
					div2.id = "answerBox";
					div2.innerHTML='<span class="single-answer">'+value+'</span><input id="delete" name="'+value+'" type="submit" value="X" onclick="deleteDiv(this.name)">';
					div1.appendChild(div2);
					wordsCount++;
				}else{
					document.getElementById("answer_textbox").style.background="#dfe9ec";
					document.getElementById("answer_textbox").disabled=true;
				}
			}
			
		}
		document.getElementById("answer_textbox").value = "";
			
		
	}
	
	function addTaboworddiv(){
		var taboWordarray;
		taboWords.refresh();
		taboWordarray=taboWords.getTaboWords();
		
		a = $.makeArray(taboWordarray);
		var div1=document.getElementById("tabooWords");
		div1.innerHTML="";
		
		for(i in taboWordarray){
			var div1=document.getElementById("tabooWords");
			var div2=document.createElement("div");
			div2.id = "tabooBox";
			div2.name = taboWordarray[i];
			div2.innerHTML='<span class="single-taboword">'+taboWordarray[i]+'</span>';
			div1.appendChild(div2);
		}
	}
	function emptyWordsContainer(){
		var div1=document.getElementById("wordGroup");
		div1.innerHTML="";
		document.getElementById("answer_textbox").value = "";
	}
	
	
	function sendAnswers(){
		// Add picture id to array
		var array = [n-1];
		array=array.concat(answerArray.getAnswerArray());

		// Send answers to database
		var result = JSON.stringify(array);
		
		
		$.ajax({
				async: false,
				url: "functions.php",
				type: "POST",
				dataType: "json",
				data: {
					sendAnswers: result
				},
				success: function(data){
					console.log(data);					
				},
				error: function(err1, err2, err3){
				console.warn(err1);
				console.warn(err2);
				console.error(err3);
			}
			});
		loadPicture();
	}
	
	function deleteDiv(name){
		$("#wordGroup").find('input[name='+name+']').parent().remove()
	}
	
	var user = (function(){
			var isloggedin;
			var score;
			var username;
			function load(){
				$.ajax({
						async: false,
						url: "functions.php",
						type: "POST",
						dataType: "json",
						data: {
							isLoggedin: "result"
						},
						success: function(data){
							isloggedin= data;		
						},
						error: function(err1, err2, err3){
						console.warn(err1);
						console.warn(err2);
						console.error(err3);
					}
				});
			}
			function getScore(){
				
				$.ajax({
						async: false,
						url: "functions.php",
						type: "POST",
						dataType: "json",
						data: {
							getScore: "result"
						},
						success: function(data){
							score= data;		
						},
						error: function(err1, err2, err3){
						console.warn(err1);
						console.warn(err2);
						console.error(err3);
					}
				});
			}
			function getUser(){
				
				$.ajax({
						async: false,
						url: "functions.php",
						type: "POST",
						dataType: "json",
						data: {
							getUsername: "result"
						},
						success: function(data){
							username= data;		
						},
						error: function(err1, err2, err3){
						console.warn(err1);
						console.warn(err2);
						console.error(err3);
					}
				});
			}
			
			return{
				load :function(){
					if(isloggedin) return;
					load();
				},
				isLoggedIn : function(){
					if(!isloggedin) load();
					return isloggedin;
				},
				refresh: function(){
					load();
				},
				getScore : function(){
					if(!score) getScore();
					return score;
				},
				getUsername : function(){
					if(!username) getUser();
					return username;
				}			
			}
		})(); 
	
	
	function isLoggedIn(){
		
	  if(!user.isLoggedIn()){
	  	window.location.replace("./index.html");
	  }
	}
	
	
	
	$(document).keypress(function(e){
    if (e.which == 13){
        $("#add").click();
    }
	});
	
	function loadScore(){
		$('#scoreNumber').text(user.getScore());
		
	}
	
	function loadPicture(){
		loadScore();
		emptyWordsContainer();
		getPicture();
		addTaboworddiv();
		wordsCount=0;
	}
	
	
	
	
	window.onload=function(){
		$('#username').text(user.getUsername());
		

		
		maxN = maxNdata.getMaxN();
		loadPicture();
		
	}
	
	$( document ).ready( isLoggedIn());